package com.agileboot.attendance.domain.acceptance.db.service.impl;

import com.agileboot.attendance.domain.acceptance.db.service.AttAcceptanceService;
import com.agileboot.attendance.domain.acceptance.dto.AcceptanceQuery;
import com.agileboot.attendance.domain.acceptance.entity.AttAcceptanceEntity;
import com.agileboot.attendance.domain.acceptance.db.mapper.AttAcceptanceMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 验收单数据库服务实现类
 */
@Primary
@Service
public class AttAcceptanceServiceImpl extends ServiceImpl<AttAcceptanceMapper, AttAcceptanceEntity> implements AttAcceptanceService {

    @Override
    public Page<AttAcceptanceEntity> selectPage(AcceptanceQuery query) {
        Page<AttAcceptanceEntity> page = new Page<>(query.getPageNum(), query.getPageSize());
        
        LambdaQueryWrapper<AttAcceptanceEntity> wrapper = new LambdaQueryWrapper<>();
        
        // 项目ID查询
        if (query.getProjectId() != null) {
            wrapper.eq(AttAcceptanceEntity::getProjectId, query.getProjectId());
        }
        
        // 验收标题模糊查询
        if (StringUtils.hasText(query.getTitle())) {
            wrapper.like(AttAcceptanceEntity::getTitle, query.getTitle());
        }
        
        // 验收状态查询
        if (query.getStatus() != null) {
            wrapper.eq(AttAcceptanceEntity::getStatus, query.getStatus());
        }
        
        // 验收时间范围查询
        if (query.getStartTime() != null) {
            wrapper.ge(AttAcceptanceEntity::getAcceptanceTime, query.getStartTime());
        }
        if (query.getEndTime() != null) {
            wrapper.le(AttAcceptanceEntity::getAcceptanceTime, query.getEndTime());
        }
        
        // 只查询未删除的记录
        wrapper.eq(AttAcceptanceEntity::getDeleted, 0);
        
        // 按创建时间降序排序
        wrapper.orderByDesc(AttAcceptanceEntity::getCreateTime);
        
        return this.page(page, wrapper);
    }
} 